package com.libgdxbar.pvzlikegame.screens.gamescreens;

import com.badlogic.gdx.utils.Array;
import com.badlogic.gdx.utils.ScreenUtils;
import com.libgdxbar.pvzlikegame.AssetsLoader;
import com.libgdxbar.pvzlikegame.entities.RectTouchableEntity;
import com.libgdxbar.pvzlikegame.entities.GameWorld;
import com.libgdxbar.pvzlikegame.objects.*;
import com.libgdxbar.pvzlikegame.screens.GameScreenAdapter;
import com.libgdxbar.pvzlikegame.systems.GameCameraSystem;
import org.libgdxbar.projects.interfaces.ICollision;

import java.util.ArrayList;
import java.util.List;

public class ShapeAndTextureEntityScreen extends GameScreenAdapter {
	List<RectTouchableEntity> clickableEntities = new ArrayList<RectTouchableEntity>();

	@SuppressWarnings("NewApi")
	@Override
	public void create() {
		// 这样创建实体 示例植物和示例僵尸都是实体
		new SampleAnimationPlant(200, 100);
		new SampleTexturePlant(200, 200);
		new SampleTextureAsync(500, 500);
		Array<ICollision> zombies = new Array<>(new ICollision[]{
			new SampleShapeZombie(300, 300),
			new SampleShapeZombie(300, 400),
			new SampleShapeZombie(400, 300),
			new SampleShapeZombie(400, 400),
		});
		zombies.forEach(e -> ((SampleShapeZombie) e).setCollisionEntities(zombies));

		//点击左下角才会触发
		/*RectTouchableEntity rectTouchableEntity = new RectTouchableEntity(new Rectangle(0, 0, 100, 100)) {
			protected InputProcessor setInputListener() {
				return new InputAdapter() {
					Vector2 worldCoord = new Vector2();

					@Override
					public boolean touchDown(int screenX, int screenY, int pointer, int button) {
						//TODO 这里需要实际转换为世界坐标
						worldCoord.set(screenX, screenY);
						screenToWorldCoord(worldCoord);
						int x = (int) worldCoord.x;
						int y = (int) worldCoord.y;
						boolean inBound = getRect().contains(x, y);
						Gdx.app.log("info", String.format(
							"clickableEntity被点击: { 点击坐标: [x: %d, y: %d], 转屏幕坐标: [x: %d, y: %d], rect: [x: %.2f, y: %.2f, width: %.2f, height: %.2f], 是否击中: %s }"
							, screenX, screenY, x, y, getRect().x, getRect().y, getRect().width, getRect().height, inBound
						));
						if (inBound)
							onClick(x, y);
						return true;
					}
				};
			}

			public boolean onClick(int x, int y) {
				// 演示逻辑: 点击在对应位置添加对象
				SampleShapeZombie zombie = new SampleShapeZombie(getViewCenter().x, getViewCenter().y);
				zombies.add(zombie);
				zombie.setCollisionEntities(zombies);
				return true;
			}
		};
		clickableEntities.add(rectTouchableEntity);*/
		for (RectTouchableEntity rectTouchableEntity1 : clickableEntities) {
			getImp().addProcessor(rectTouchableEntity1.getInputListener());
		}
		int k;
	}

	@Override
	public void show() {
		super.show();
		GameWorld.getInstance().getSystem(GameCameraSystem.class).setCamera(getViewport().getCamera());
	}

	@Override
	public void render(float delta) {
		ScreenUtils.clear(0.2f, 0.2f, 0.2f, 1f);
		GameWorld.getInstance().update(delta);
	}

	@Override
	public void dispose() {
		GameWorld.getInstance().dispose();
		AssetsLoader.getInstance().dispose();
	}
}
